package cetcbigdata.da.service.hainan;

import cetcbigdata.da.base.Base;
import cetcbigdata.da.service.hainan.util.AllInfo;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * 政务公开
 * 省级备案事项清单
 */

public class ShengJiBeiAnShiXiangList extends Base {
    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return this.parseListPage();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject parseListPage() {
        //创建获取详情页信息的对象
        AllInfo allInfo = new AllInfo();
        //当前页
        int currentPage = 1;
        //总页数
        int pages = 0;
        while (currentPage != pages){
            String listUrl = "https://wssp.hainan.gov.cn/wssp3/zwgk/getSxListByTopicIdAndDeptId?" +
                    "deptId=&" +
                    "topicId=d830f0e403fc4aeea888d917ad583cc6&" +
                    "taskName=&" +
                    "pageNumber="+currentPage+"&" +
                    "pageSize=10&" +
                    "xzqhId=HZ2881f4424539dd0142453c856b0025";
            JSONObject listHeader = new JSONObject();
            listHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
            listHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/sxInformPromise_detail-sjSxBaSxQd?qdCategory=sjSxBaSxQd&title=");
            JSONObject listParmas = new JSONObject();
            String strResponse = postFormReq(listUrl,listHeader,listParmas);
            JSONObject response = JSONObject.parseObject(strResponse);
            for (Object o : response.getJSONObject("data").getJSONArray("datas")){
                JSONObject reqData = (JSONObject) o;
                allInfo.setSourcekey(reqData.getString("sourcekey"));
                allInfo.setId("1");
                allInfo.init();
                JSONObject result;
                try {
                    result = allInfo.getAllInfo();
                } catch (Exception e) {
                    break;
                }
                System.out.println(result);//测试用代码
                return processResult(
                        null,
                        null,
                        result.getString("地址"),
                        allInfo.getResponce(),
                        result,
                        "海南省",
                        "政务公开-省级备案事项清单"
                );
            }
            //更新当前页和总页数
            currentPage++;
            pages = response.getJSONObject("data").
                    getJSONObject("page").getInteger("pageCount") + 1;
        }
        return null;
    }

    public static void main(String[] args) {
        ShengJiBeiAnShiXiangList shengJiBeiAnShiXiangList = new ShengJiBeiAnShiXiangList();
        shengJiBeiAnShiXiangList.getDataDemo();
    }
}
